package com.wy.about_data_share.demo5;
//想办法让子线程和主线程的引用指向同一个对象-利用构造方法
public class Main {
   static class SomeObject{
       int r=0;
   }
    static class MyThread extends Thread{
       private final SomeObject someObject;
       public MyThread(SomeObject someObject){
           this.someObject=someObject;//这个someObject指向主线程main方法栈的someObject指向的对象
       }
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                someObject.r++;
            }
            System.out.println(getName()+":"+someObject.r);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        SomeObject someObject=new SomeObject();//只有这个地方创建对象
        MyThread t=new MyThread(someObject);//把SomeObject对象的引用传入
        t.start();
        t.join();
        System.out.println(Thread.currentThread().getName()+":"+someObject.r);
    }
}
